package lu.med.praxiswahl.wahlumed.dao.beans;

import java.util.List;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import lu.med.praxiswahl.wahlumed.dao.beans.daointerface.ScanDAOInt;
import lu.med.praxiswahl.wahlumed.model.beans.CustomerTBL;
import lu.med.praxiswahl.wahlumed.model.beans.ScanTBL;

import javax.ejb.Local;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
* Session Bean implementation class ScanDAO
* @author Patrick kendzo
* Email: pkendzo@msn.com
*/
 
@Stateless
@Local({ScanDAOInt.class})
public class ScanDAO implements ScanDAOInt {

	@PersistenceContext(unitName = "gynaekomedic-ejbPU")
    private EntityManager em = null;
	
    /**
     * Default constructor. 
     */
    public ScanDAO() {
     
    }

	@Override
	public void delete(Long todo)
	{
		ScanTBL item = em.find(ScanTBL.class, todo);

        CustomerTBL cust = item.getCustomer();

        cust.getScanEintraege().remove(item);

        em.remove(item);
        em.flush();
		
	}

	@Override
	public void persist(ScanTBL todo)
	{
		em.persist(todo);
        em.flush();
		
	}

	@Override
	public ScanTBL retrieveScanItemByID(Long id)
	{
		return em.find(ScanTBL.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ScanTBL> retrieveScanItemsByCustomer(
			String matricule)
	{
		 try {
	            return (List<ScanTBL>) em.createQuery(
	                    "select s from ScanTBL s where s.customer = '" + matricule + "' order by f.datum desc").getResultList();
	        } catch (Exception e) {
	            throw new RuntimeException(
	                    "Could not retrieve scan item for this Customer", e);
	        }
	}

	@Override
	public void update(ScanTBL todo)
	{
		em.merge(todo);
        em.flush();
		
	}

	@Override
	public Long findSubPrimaryKeyIndexBYTyp(int typ)
	{
		try
		{
			
			Long result = (Long)em
					.createQuery(
							"select count(typ) from ScanTBL s where s.typ = "+typ+" order by s.datum desc").getSingleResult();
			
			
			return result.longValue();
		}
		catch (Exception e)
		{
			/*throw new RuntimeException("Houston we have a problem",
					e);*/
			return 0l;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ScanTBL> retrieveScanBY_Typ(String typ)
	{
		try {
            return (List<ScanTBL>) em.createQuery(
                    "select s from ScanTBL s where s.typ = '" + typ + "' order by s.datum desc").getResultList();
        } catch (Exception e) {
            throw new RuntimeException(
                    "Could not retrieve scan items for this type", e);
        }
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ScanTBL> retrieveScan_BYCustomer_and_BYtyp(
			String matricule, String typ)
	{
		try {
            return (List<ScanTBL>) em.createQuery(
                    "select s from ScanTBL s where s.customer = '" + matricule + "' and s.typ = '"+typ+"'" +
                    		"order by s.datum desc").getResultList();
        } catch (Exception e) {
            throw new RuntimeException(
                    "Could not retrieve the needed Formulars", e);
        }
	}

}
